<?php

class InvoicesController extends AppController {

    var $name = 'Invoices';
    var $uses = array('Product', 'Cart', 'Order', 'Invoice');
    var $components = array('GoogleCheckout');

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allowedActions = array('*');
    }

    function index() {
        $this->layout = 'innerhome';
        $this->Invoice->recursive = -1;
        if (!empty($this->data)) {
            $search_year = $this->data['search_year'];
            $search_month = $this->data['search_month'];
            $start_date = date("Y-m-d H:i:s", mktime(0, 0, 0, $search_month, 1, $search_year));
            $end_date = date("Y-m-d H:i:s", mktime(0, 0, 0, $search_month + 1, 1, $search_year));
            $this->paginate = array('limit' => 25,
                'conditions' => array('Invoice.invoice_date BETWEEN ? AND ?' => array($start_date, $end_date), 'Invoice.user_id' => $this->Auth->user('id'), 'Invoice.invoice_status' => 'Invoices Awaiting Payment'),
                'order' => array('Invoice.id' => 'DESC')
            );
        } else {
            $this->paginate = array('limit' => 25,
                'conditions' => array('Invoice.user_id' => $this->Auth->user('id'), 'Invoice.invoice_status' => 'Invoices Awaiting Payment'),
                'order' => array('Invoice.id' => 'DESC')
            );
        }
        $invoices = $this->paginate('Invoice');
        //pr($invoices);
        $this->set('invoices', $invoices);
    }

    function fetch_invoice_order($id=null) {
        $invoices = $this->Invoice->findById($id);
        $sellerID = $invoices['Invoice']['user_id'];
        $all_orders = explode(",", $invoices['Invoice']['orders']);
        $invoice_date = $invoices['Invoice']['invoice_date'];
        $order_date_m1 = date("m", strtotime($invoice_date));
        $order_date_y1 = date("Y", strtotime($invoice_date));
        $order_date = date("Y-m-d H:i:s", mktime(0, 0, 0, $order_date_m1 - 1, 1, $order_date_y1));

        $order_date2 = "";
        $this->Product->recursive = -1;
        $seller_products = $this->Product->find("list", array('conditions' => array("Product.user_id" => $sellerID, "OR" => array(array("Product.products_status" => "Approved"), array("Product.products_status" => "Active"))), 'fields' => array('Product.id')));
        $this->Cart->recursive = 2;
        $i = 0;
        rsort($all_orders);
        foreach ($all_orders as $order) {
            $this->Cart->recursive = 2;
            $this->Cart->Behaviors->attach('Containable');
            $orders[$i] = $this->Cart->find('first', array(
                'contain' => array('Order.id', 'Order.add_date', 'User.id', 'User.first_name', 'User.last_name'),
                'conditions' => array('Cart.payment_status' => 1, 'Cart.order_id' => $order),
                'group' => array('Cart.order_id')
                    ));
            $this->Cart->recursive = -1;
            $sum = $this->Cart->find("first", array(
                'conditions' => array('Cart.payment_status' => 1, 'Cart.order_id' => $order),
                'fields' => array('sum((Cart.product_cost*Cart.quantity)+Cart.sales_tax+Cart.shipping_cost) AS total')
                    ));
            $orders[$i]['sum_total'] = $sum[0]['total'];
            $this->Cart->recursive = 2;
            $this->Cart->Behaviors->attach('Containable');
            $orders[$i]['products'] = $this->Cart->find('all', array(
                'contain' => array('Order.id', 'Order.transaction_id', 'Shipping.shipping_method', 'Product.id', 'Product.user_id', 'Product.category_id', 'Product.products_title', 'Product.products_thumbnailImage', 'User.id', 'User.first_name', 'User.last_name'),
                'conditions' => array('Cart.payment_status' => 1, 'Cart.order_id' => $order)
                    ));
            $i++;
        }
        $this->set(compact('orders', 'order_date', 'invoice_date'));
    }

    function backstage_index() {
        $this->layout = 'admin';

        if (!empty($this->data)) {
            $search_year = $this->data['search_year'];
            $search_month = $this->data['search_month'];
            $start_date = date("Y-m-d H:i:s", mktime(0, 0, 0, $search_month, 1, $search_year));
            $end_date = date("Y-m-d H:i:s", mktime(0, 0, 0, $search_month + 1, 1, $search_year));
            $this->paginate = array('limit' => 25,
                'conditions' => array('Invoice.invoice_date BETWEEN ? AND ?' => array($start_date, $end_date), 'Invoice.user_id' => $this->Auth->user('id'), 'Invoice.invoice_status' => 'Invoices Awaiting Payment'),
                'order' => array('Invoice.id' => 'DESC')
            );
        } else {
            $this->paginate = array('limit' => 25,
                'conditions' => array('Invoice.invoice_status' => 'Invoices Awaiting Payment'),
                'order' => array('Invoice.id' => 'DESC')
            );
        }
        $invoices = $this->paginate('Invoice');

        $this->set('invoices', $invoices);
    }

    function overdue_invoices() {
        $this->layout = 'innerhome';
        if (!empty($this->data)) {
            $search_year = $this->data['search_year'];
            $search_month = $this->data['search_month'];
            $start_date = date("Y-m-d H:i:s", mktime(0, 0, 0, $search_month, 1, $search_year));
            $end_date = date("Y-m-d H:i:s", mktime(0, 0, 0, $search_month + 1, 1, $search_year));
            $this->paginate = array('limit' => 25,
                'conditions' => array('Invoice.invoice_date BETWEEN ? AND ?' => array($start_date, $end_date), 'Invoice.user_id' => $this->Auth->user('id'), 'Invoice.invoice_status' => 'Invoices Awaiting Payment'),
                'order' => array('Invoice.id' => 'DESC')
            );
        } else {
            $this->paginate = array('limit' => 25,
                'conditions' => array('Invoice.user_id' => $this->Auth->user('id'), 'Invoice.invoice_status' => 'Overdue Invoices'),
                'order' => array('Invoice.id' => 'DESC')
            );
        }
        $this->set('invoices', $this->paginate('Invoice'));
    }

    function backstage_overdue_invoices() {
        $this->layout = 'admin';
        if (!empty($this->data)) {
            $search_year = $this->data['search_year'];
            $search_month = $this->data['search_month'];
            $start_date = date("Y-m-d H:i:s", mktime(0, 0, 0, $search_month, 1, $search_year));
            $end_date = date("Y-m-d H:i:s", mktime(0, 0, 0, $search_month + 1, 1, $search_year));
            $this->paginate = array('limit' => 25,
                'conditions' => array('Invoice.invoice_date BETWEEN ? AND ?' => array($start_date, $end_date), 'Invoice.user_id' => $this->Auth->user('id'), 'Invoice.invoice_status' => 'Invoices Awaiting Payment'),
                'order' => array('Invoice.id' => 'DESC')
            );
        } else {
            $this->paginate = array('limit' => 25,
                'conditions' => array('Invoice.invoice_status' => 'Overdue Invoices'),
                'order' => array('Invoice.id' => 'DESC')
            );
        }
        $this->set('invoices', $this->paginate('Invoice'));
    }

    function paid_invoices() {
        $this->layout = 'innerhome';
        if (!empty($this->data)) {
            $search_year = $this->data['search_year'];
            $search_month = $this->data['search_month'];
            $start_date = date("Y-m-d H:i:s", mktime(0, 0, 0, $search_month, 1, $search_year));
            $end_date = date("Y-m-d H:i:s", mktime(0, 0, 0, $search_month + 1, 1, $search_year));
            $this->paginate = array('limit' => 25,
                'conditions' => array('Invoice.pay_date BETWEEN ? AND ?' => array($start_date, $end_date), 'Invoice.user_id' => $this->Auth->user('id'), 'Invoice.invoice_status' => 'Invoices Awaiting Payment'),
                'order' => array('Invoice.id' => 'DESC')
            );
        } else {
            $this->paginate = array('limit' => 25,
                'conditions' => array('Invoice.user_id' => $this->Auth->user('id'), 'Invoice.invoice_status' => 'Paid Invoices'),
                'order' => array('Invoice.id' => 'DESC')
            );
        }
        $this->set('invoices', $this->paginate('Invoice'));
    }

    function backstage_paid_invoices() {
        $this->layout = 'admin';
        if (!empty($this->data)) {
            $search_year = $this->data['search_year'];
            $search_month = $this->data['search_month'];
            $start_date = date("Y-m-d H:i:s", mktime(0, 0, 0, $search_month, 1, $search_year));
            $end_date = date("Y-m-d H:i:s", mktime(0, 0, 0, $search_month + 1, 1, $search_year));
            $this->paginate = array('limit' => 25,
                'conditions' => array('Invoice.pay_date BETWEEN ? AND ?' => array($start_date, $end_date), 'Invoice.user_id' => $this->Auth->user('id'), 'Invoice.invoice_status' => 'Invoices Awaiting Payment'),
                'order' => array('Invoice.id' => 'DESC')
            );
        } else {
            $this->paginate = array('limit' => 25,
                'conditions' => array('Invoice.invoice_status' => 'Paid Invoices'),
                'order' => array('Invoice.id' => 'DESC')
            );
        }
        $this->set('invoices', $this->paginate('Invoice'));
    }

    function process_invoice_paypal() {
        $this->layout = false;
        if (!empty($this->data)) {
            $invoices = $this->Invoice->find("first", array("conditions" => array("Invoice.id" => $this->data['Invoice']['id'], "Invoice.invoice_status" => "Invoices Awaiting Payment")));
        }
        $this->loadModel('Paymentmethod');
        $paymentsGateway = $this->Paymentmethod->find("first", array("conditions" => array("Paymentmethod.user_id" => 1, "Paymentmethod.methodname" => "Paypal")));
        $this->set(compact('invoices', 'paymentsGateway'));
    }

    function process_invoice_gcheckout() {
        $this->layout = false;
        if (!empty($this->data)) {
            $invoices = $this->Invoice->find("first", array("conditions" => array("Invoice.id" => $this->data['Invoice']['id'], "Invoice.invoice_status" => "Invoices Awaiting Payment")));
            if ($invoices['Invoice']['aa_fee'] > 0) {
                $this->Session->write('payToId', "1");
                $MerchantPrivateData = array("cart-id" => $invoices['Invoice']['id'], "paymentType" => "invoice");
                $this->GoogleCheckout->setItem("Authentic Avenue fee", "Authentic Avenue fee", 1, $invoices['Invoice']['aa_fee'], $MerchantPrivateData, $MerchantPrivateData);
                $invoices['Invoice']['gcheckout'] = $this->GoogleCheckout->generateCartButton($MerchantPrivateData);
            }
        }

        $this->set(compact('invoices'));
    }

}

?>